//filename receiver.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_MSG_TEXT 100
#define MSG_QUEUE_KEY 1235
struct my_msgbuf {
    long mtype;
    char mtext[MAX_MSG_TEXT];
};
int main() {
    int msqid;
    key_t key;
    struct my_msgbuf buf;
    
    // 使用相同的键值获取消息队列
    key = MSG_QUEUE_KEY;

    // 打开已经存在的消息队列
    if ((msqid = msgget(key, 0666)) == -1) {
        perror("msgget");
        exit(1);
    }
    
    while(1) {
        // 接收消息
        if (msgrcv(msqid, &buf, MAX_MSG_TEXT, 1, 0) == -1) {
            perror("msgrcv");
            exit(1);
        }
        
        printf("Received message: %s\n", buf.mtext);
        sleep(2);
    }
    
    return 0;
}